<?php
/**
 * 用户作品集
 *
 * 用户作品集controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Api;

use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\UserCollectionsService\UserCollectionsService;
use Illuminate\Support\Facades\Response;

class UserCollectionsController extends Controller
{
    protected $userCollectionsService;
    public function __construct(UserCollectionsService $collectionsService)
    {
        $this->userCollectionsService = $collectionsService;
    }

    /**
     * 创建作品集
     */
    public function postCreateConllection(Request $request){
        $check_data = array(
            'original_image'=>'required|active_url',
            'thumbnail' => 'required|active_url',
            'user_id'=>'required|integer',
            'collection_name'=>'required|string|max:50'
        );
        $v = $this->userCollectionsService->store($request,$check_data);
        if ($v->fails())
        {
            if ($v->errors()->first('original_image') || $v->errors()->first('thumbnail')) {
                $resultMes = $this->userCollectionsService->createResult('M09030',(object)array());
            }elseif($v->errors()->first('user_id')) {
                $resultMes = $this->userCollectionsService->createResult('M09005',(object)array());
            }else{
                $resultMes = $this->userCollectionsService->createResult('M09031',(object)array());
            }
            return Response::json($resultMes);
        }
        $original_image = trim($request->input('original_image'));
        $thumbnail = trim($request->input('thumbnail'));
        $user_id = intval($request->input('user_id'));
        $collection_name = trim($request->input('collection_name'));
        $is_collection_valid = GeneralTool::hasSpecialChar($collection_name);
        if($is_collection_valid){
           return Response::json($this->userCollectionsService->createResult('M09031',(object)array()));
        }
        if($collection_name == '默认作品集'){
            return Response::json($this->userCollectionsService->createResult('M09037',(object)array()));
        }
        $result = $this->userCollectionsService->createUserCollection(array('original_image'=>$original_image,
            'thumbnail'=>$thumbnail,'user_id'=>$user_id,'collection_name'=>$collection_name));
        return Response::json($result);
    }

    /**
     * 获取作品集
     * @param Request $request
     * @return mixed
     */
    public function postList(Request $request){
        $check_data = array('user_id'=>'required|integer');
        $v = $this->userCollectionsService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('user_id')) {
                $resultMes = $this->userCollectionsService->createResult('M09005',(object)array());
                return Response::json($resultMes);
            }
        }
        $user_id = intval($request->input('user_id'));
        $per_page = intval($request->input('per_page')) <= 0 ? 10 : intval($request->input('per_page'));
        $result = $this->userCollectionsService->getList($user_id,$per_page);
        return Response::json($result);
    }

    /**
     * 更新
     * @param Request $request
     * @return mixed
     */
    public function postUpdateCollection(Request $request){
        $check_data = array(
            'collection_id'=>'required|alpha_dash|max:32',
            'original_image'=>'required|active_url',
            'thumbnail' => 'required|active_url',
            'user_id'=>'required|integer',
            'collection_name'=>'required|string|max:50');
        $v = $this->userCollectionsService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('collection_id')){
                $resultMes = $this->userCollectionsService->createResult('M09034',(object)array());
            } elseif ($v->errors()->first('original_image') || $v->errors()->first('thumbnail')) {
                $resultMes = $this->userCollectionsService->createResult('M09030',(object)array());
            }elseif($v->errors()->first('user_id')) {
                $resultMes = $this->userCollectionsService->createResult('M09005',(object)array());
            }else{
                $resultMes = $this->userCollectionsService->createResult('M09031',(object)array());
            }
            return Response::json($resultMes);
        }
        $collection_id = trim($request->input('collection_id'));
        $original_image = $request->input('original_image');
        $thumbnail = $request->input('thumbnail');
        $user_id = intval($request->input('user_id'));
        $collection_name = trim($request->input('collection_name'));
        $is_collection_valid = GeneralTool::hasSpecialChar($collection_name);
        if($is_collection_valid){
            return Response::json($this->userCollectionsService->createResult('M09031',(object)array()));
        }
        if($collection_name == '默认作品集'){
            return Response::json($this->userCollectionsService->createResult('M09037',(object)array()));
        }
        $result = $this->userCollectionsService->updateUserCollection(array('collection_id'=>$collection_id,
            'original_image'=>$original_image,'thumbnail'=>$thumbnail,'user_id'=>$user_id,'collection_name'=>$collection_name));
        return Response::json($result);
    }

    public function postDelete(Request $request){
        $check_data = array(
            'collection_id'=>'required|alpha_dash|max:32',
            'user_id'=>'required|integer'
        );
        $v = $this->userCollectionsService->store($request,$check_data);
        if ($v->fails())
        {
            if($v->errors()->first('collection_id')){
                $resultMes = $this->userCollectionsService->createResult('M09034',(object)array());
            } else {
                $resultMes = $this->userCollectionsService->createResult('M09005',(object)array());
            }
            return Response::json($resultMes);
        }
        $collection_id = trim($request->input('collection_id'));
        $user_id = intval($request->input('user_id'));
        $result = $this->userCollectionsService->deleteCollection($user_id,$collection_id);
        return Response::json($result);
    }
}
